var my_audio = document.getElementById("audio");
var p_all ;
var startX = startY = endX = endY = 0;

setTimeout(function () {
    p_all = $("#proline")[0].offsetWidth;
},0)

my_audio.ontimeupdate = function () {
    //获取当前播放时间
    //document.getElementById("now_time").innerHTML = timeFormat(my_audio.currentTime);
    //当前的长度
    now_long = my_audio.currentTime / my_audio.duration * p_all;
    $(".bar").css({width: now_long});
    var btn_l = now_long - 10 + 'px';
    $(".btn").css({left: btn_l});
    addListenTouch();
};
my_audio.onprogress = function () {
    //document.getElementById("all_time").innerHTML = timeFormat(my_audio.duration);
};
var timeFormat = function (seconds) {
    var m = Math.floor(seconds / 60) < 10 ? "0" + Math.floor(seconds / 60) : Math.floor(seconds / 60);
    var s = Math.floor(seconds - (m * 60)) < 10 ? "0" + Math.floor(seconds - (m * 60)) : Math.floor(seconds - (m * 60));
    return m + ":" + s;
};
//手动拉拽进度条的部分
function addListenTouch() {
    //var speed=$('.had-play');
    var btn = document.getElementById("btn");
    document.getElementById("btn").addEventListener("touchstart", touchStart, false);
    document.getElementById("btn").addEventListener("touchmove", touchMove, false);
    document.getElementById("btn").addEventListener("touchend", touchEnd, false);
}
function touchStart(e) {
    e.preventDefault();
    var touch = e.touches[0];
    startX = touch.pageX;
    my_audio.pause();
    //document.getElementById("all_time").innerHTML = timeFormat(my_audio.duration);
}
function touchMove(e) {//滑动
    e.preventDefault();
    var touch = e.touches[0];
    x = touch.pageX - startX//滑动的距离
    var widthBar = now_long + x;
    $(".bar").css({width: widthBar});
    if (widthBar < p_all) {
        $("#btn").css({left: widthBar - 10 + 'px'});
        $("#bar").css({width: widthBar});
    }
    var yu = widthBar / p_all * my_audio.duration;
    document.getElementById("curTime").innerHTML = timeFormat(yu);
}
function touchEnd(e) { //手指离开屏幕
    e.preventDefault(); //取消事件的默认动作
    now_long = parseInt(btn.style.left);
    var touch = e.touches[0];
    var dragPaddingLeft = btn.style.left;
    var change = dragPaddingLeft.replace("px", "");
    numDragpaddingLeft = parseInt(change);
    var currentTimeNew = (numDragpaddingLeft / (p_all - 20) * my_audio.duration);
    console.log(timeFormat(currentTimeNew))
    my_audio.currentTime = parseInt(currentTimeNew);
    my_audio.play();
    document.getElementById("curTime").innerHTML = timeFormat(currentTimeNew);

    $("#circle").addClass("rainbow")
}